{
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
}

unit OrderQuestionOptions;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Spin, ExtCtrls, Scene, superobject, Dispatcher, VisualUtils;

type
  TOrderQuestionOptionsForm = class(TForm)
    ApplicationProperties: TApplicationProperties;
    btnOk: TButton;
    btnCancel: TButton;
    gbxItems: TGroupBox;
    gbxDistractors: TGroupBox;
    pnlButtons: TPanel;
    rbAllItems: TRadioButton;
    rbAllDistractors: TRadioButton;
    rbRandomItems: TRadioButton;
    rbRandomDistractors: TRadioButton;
    spinItemLimit: TSpinEdit;
    spinDistractorLimit: TSpinEdit;
    procedure ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
    procedure btnOkClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure rbRandomDistractorsClick(Sender: TObject);
    procedure rbRandomItemsClick(Sender: TObject);
  private
    FId: TShapeId;
    FOptions: ISuperObject;
    FDispatcher: TDispatcher;
    FSelectControl: TWinControl;
    procedure Prepare;
    { private declarations }
  public
    class procedure OpenModal(const Id: TShapeId; const Options: ISuperObject; Dispatcher: TDispatcher);
    { public declarations }
  end; 

implementation

{$R *.lfm}

{ TOrderQuestionOptionsForm }

procedure TOrderQuestionOptionsForm.FormShow(Sender: TObject);
begin
  StretchToWidest([btnOk, btnCancel]);
  SetSpinEditWidthAndMaxLengthByMaxValue(spinItemLimit);
  SetSpinEditWidthAndMaxLengthByMaxValue(spinDistractorLimit);
end;

procedure TOrderQuestionOptionsForm.rbRandomDistractorsClick(Sender: TObject);
begin
  FSelectControl := spinDistractorLimit;
end;

procedure TOrderQuestionOptionsForm.rbRandomItemsClick(Sender: TObject);
begin
  FSelectControl := spinItemLimit;
end;

procedure TOrderQuestionOptionsForm.ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
begin
  spinItemLimit.Enabled := rbRandomItems.Checked;
  spinDistractorLimit.Enabled := rbRandomDistractors.Checked;
  if FSelectControl <> nil then
    TrySetFocus(FSelectControl);
  FSelectControl := nil;
end;

procedure TOrderQuestionOptionsForm.btnOkClick(Sender: TObject);
var
  Input: ISuperObject;
begin
  Input := SO;
  if rbRandomItems.Checked then
    Input.I['itemLimit'] := spinItemLimit.Value;
  if rbRandomDistractors.Checked then
    Input.I['distractorLimit'] := spinDistractorLimit.Value;

  FDispatcher.PerformAction(FId, 'setOptions', Input);
  ModalResult := mrOk;
end;

procedure TOrderQuestionOptionsForm.Prepare;
var
  v: ISuperObject;
begin
  spinItemLimit.MaxValue := FOptions.I['maxElements'];
  spinDistractorLimit.MaxValue := FOptions.I['maxElements'];

  v := FOptions['itemLimit'];
  if v <> nil then
  begin
    SetRadioButtonStateWithoutOnClick(rbRandomItems, TRUE);
    spinItemLimit.Value := v.AsInteger;
  end;

  v := FOptions['distractorLimit'];
  if v <> nil then
  begin
    SetRadioButtonStateWithoutOnClick(rbRandomDistractors, TRUE);
    spinDistractorLimit.Value := v.AsInteger;
  end;
end;

class procedure TOrderQuestionOptionsForm.OpenModal(const Id: TShapeId;
  const Options: ISuperObject; Dispatcher: TDispatcher);
var
  Form: TOrderQuestionOptionsForm;
begin
  Form := TOrderQuestionOptionsForm.Create(nil);
  try
    RestoreFormPosition(Form);

    Form.FId := Id;
    Form.FOptions := Options;
    Form.FDispatcher := Dispatcher;
    Form.Prepare;

    ShowModalForm(Form);
  finally
    Form.Free;
  end;
end;

end.

